<template>
  <view class="demand-detail">
    <!-- 渐变背景装饰 -->
    <view class="header-bg">
      <view class="bg-shape bg-shape-1"></view>
      <view class="bg-shape bg-shape-2"></view>
      <view class="bg-shape bg-shape-3"></view>
    </view>

    <!-- 自定义导航栏 -->
    <view class="custom-navbar">
      <view class="navbar-content">
        <view class="navbar-left" @click="goBack">
          <u-icon name="arrow-left" color="#fff" size="20"></u-icon>
        </view>
        <view class="navbar-title">需求详情</view>
        <view class="navbar-right">
          <u-icon name="share" color="#fff" size="20"></u-icon>
        </view>
      </view>
    </view>

    <!-- 加载状态 -->
    <view v-if="loading" class="loading-container">
      <u-loading-page 
        loading-text="加载中..." 
        font-size="16"
        loading-color="#3cc51f"
      ></u-loading-page>
    </view>

    <!-- 需求详情内容 -->
    <view v-else-if="demandInfo" class="detail-content">
      <!-- 需求基本信息 -->
      <view class="demand-card">
        <view class="demand-header">
          <view class="demand-title">{{ demandInfo.title }}</view>
          <view class="demand-status" :class="[
            demandInfo.status === 'PUBLISHED' ? 'status-published' : '',
            demandInfo.status === 'MATCHED' ? 'status-matched' : '',
            demandInfo.status === 'CLOSED' ? 'status-closed' : '',
            demandInfo.status === 'EXPIRED' ? 'status-expired' : ''
          ]">
            {{ getStatusText(demandInfo.status) }}
          </view>
        </view>
        
        <view class="demand-meta">
          <view class="meta-item">
            <u-icon name="grid-fill" size="32" color="#666"></u-icon>
            <text class="meta-text">{{ demandInfo.subject }}</text>
          </view>
          <view class="meta-item" v-if="demandInfo.grade">
            <u-icon name="grid-fill" size="32" color="#666"></u-icon>
            <text class="meta-text">{{ demandInfo.grade }}</text>
          </view>
          <view class="meta-item" v-if="demandInfo.teachingType">
            <u-icon name="grid-fill" size="32" color="#666"></u-icon>
            <text class="meta-text">{{ getTeachingTypeText(demandInfo.teachingType) }}</text>
          </view>
        </view>

        <!-- 预算信息 -->
        <view class="budget-section" v-if="demandInfo.budgetMin || demandInfo.budgetMax">
          <view class="budget-title">预算范围</view>
          <view class="budget-amount">
            <text class="currency">¥</text>
            <text class="amount">
              <template v-if="demandInfo.budgetMin && demandInfo.budgetMax">
                {{ demandInfo.budgetMin }} - {{ demandInfo.budgetMax }}
              </template>
              <template v-else-if="demandInfo.budgetMin">
                {{ demandInfo.budgetMin }} 起
              </template>
              <template v-else>
                {{ demandInfo.budgetMax }} 以内
              </template>
            </text>
            <text class="unit">/小时</text>
          </view>
        </view>
      </view>

      <!-- 需求描述 -->
      <view class="content-card" v-if="demandInfo.contentMd">
        <view class="card-title">需求描述</view>
        <view class="content-text">{{ demandInfo.contentMd }}</view>
      </view>

      <!-- 其他信息 -->
      <view class="info-card">
        <view class="card-title">详细信息</view>
        
        <!-- 上课地址 -->
        <view class="info-item" v-if="demandInfo.address">
          <view class="info-label">
            <u-icon name="map-fill" size="36" color="#3cc51f"></u-icon>
            <text>上课地址</text>
          </view>
          <text class="info-value">{{ demandInfo.address }}</text>
        </view>

        <!-- 期望时间 -->
        <view class="info-item" v-if="preferredTimeList.length">
          <view class="info-label">
            <u-icon name="clock" size="36" color="#3cc51f"></u-icon>
            <text>期望时间</text>
          </view>
          <view class="time-tags">
            <u-tag 
              v-for="(time, index) in preferredTimeList" 
              :key="index"
              :text="time" 
              size="mini" 
              type="primary"
              plain
            ></u-tag>
          </view>
        </view>

        <!-- 特殊要求 -->
        <view class="info-item" v-if="demandInfo.requirements">
          <view class="info-label">
            <u-icon name="question-circle-fill" size="36" color="#3cc51f"></u-icon>
            <text>特殊要求</text>
          </view>
          <text class="info-value">{{ demandInfo.requirements }}</text>
        </view>

        <!-- 发布时间 -->
        <view class="info-item">
          <view class="info-label">
            <u-icon name="calendar" size="36" color="#3cc51f"></u-icon>
            <text>发布时间</text>
          </view>
          <text class="info-value">{{ formatTime(demandInfo.createdAt) }}</text>
        </view>

        <!-- 过期时间 -->
        <view class="info-item" v-if="demandInfo.expiresAt">
          <view class="info-label">
            <u-icon name="close-circle-fill" size="36" color="#ff6b6b"></u-icon>
            <text>过期时间</text>
          </view>
          <text class="info-value expire-time">{{ formatTime(demandInfo.expiresAt) }}</text>
        </view>
      </view>

      <!-- 统计信息 -->
      <view class="stats-card">
        <view class="stats-item">
          <view class="stats-number">{{ demandInfo.viewCount || 0 }}</view>
          <view class="stats-label">浏览次数</view>
        </view>
        <view class="stats-item">
          <view class="stats-number">{{ demandInfo.replyCount || 0 }}</view>
          <view class="stats-label">回复次数</view>
        </view>
      </view>
    </view>

    <!-- 底部操作按钮 -->
    <view class="bottom-actions" v-if="demandInfo && demandInfo.status === 'PUBLISHED'">
      <view class="action-btn contact-btn" @click="contactParent">
        <u-icon name="chat" size="36" color="white"></u-icon>
        <text>联系家长</text>
      </view>
      <view class="action-btn apply-btn" @click="applyDemand">
        <u-icon name="file-text-fill" size="36" color="white"></u-icon>
        <text>我要接单</text>
      </view>
    </view>

    <!-- 安全区域 -->
    <view class="safe-bottom"></view>
  </view>
</template>

<script>
import { getDemandDetail } from '@/api/demand'
import { checkLogin } from '@/utils/auth'

export default {
  data() {
    return {
      demandId: null,
      demandInfo: null,
      loading: true,
      preferredTimeList: []
    }
  },

  onLoad(options) {
    if (options.id) {
      this.demandId = options.id
      this.loadDemandDetail()
    } else {
      uni.showToast({
        title: '参数错误',
        icon: 'error'
      })
      setTimeout(() => {
        uni.navigateBack()
      }, 1500)
    }
  },

  methods: {
    // 加载需求详情
    async loadDemandDetail() {
      try {
        this.loading = true
        const res = await getDemandDetail(this.demandId)
        if (res.code === 200) {
          this.demandInfo = res.data
          this.parsePreferredTime()
        } else {
          uni.showToast({
            title: res.message || '加载失败',
            icon: 'error'
          })
        }
      } catch (error) {
        console.error('加载需求详情失败:', error)
        uni.showToast({
          title: '网络错误',
          icon: 'error'
        })
      } finally {
        this.loading = false
      }
    },

    // 解析期望时间
    parsePreferredTime() {
      if (this.demandInfo.preferredTime) {
        try {
          const timeData = JSON.parse(this.demandInfo.preferredTime)
          this.preferredTimeList = Array.isArray(timeData) ? timeData : []
        } catch (error) {
          console.error('解析期望时间失败:', error)
          this.preferredTimeList = []
        }
      }
    },

    // 获取状态文本
    getStatusText(status) {
      const statusMap = {
        'PUBLISHED': '已发布',
        'MATCHED': '已匹配',
        'CLOSED': '已关闭',
        'EXPIRED': '已过期'
      }
      return statusMap[status] || status
    },



    // 获取授课方式文本
    getTeachingTypeText(type) {
      const typeMap = {
        'ONLINE': '线上授课',
        'OFFLINE': '线下授课',
        'BOTH': '线上线下均可'
      }
      return typeMap[type] || type
    },

    // 格式化时间
    formatTime(time) {
      if (!time) return ''
      const date = new Date(time)
      return date.toLocaleString('zh-CN', {
        year: 'numeric',
        month: '2-digit',
        day: '2-digit',
        hour: '2-digit',
        minute: '2-digit'
      })
    },

    // 联系家长
    contactParent() {
      if (!checkLogin()) {
        uni.navigateTo({
          url: '/pages/auth/login'
        })
        return
      }

      uni.showToast({
        title: '功能开发中',
        icon: 'none'
      })
    },

    // 申请需求
    applyDemand() {
      if (!checkLogin()) {
        uni.navigateTo({
          url: '/pages/auth/login'
        })
        return
      }

      uni.showToast({
        title: '功能开发中',
        icon: 'none'
      })
    },

    // 返回
    goBack() {
      uni.navigateBack()
    }
  }
}
</script>

<style lang="scss" scoped>
.demand-detail {
  min-height: 100vh;
  background: #f5f7fa;
  position: relative;
}

/* 渐变背景装饰 */
.header-bg {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 500rpx;
  background: linear-gradient(135deg, #3cc51f 0%, #2ea516 100%);
  overflow: hidden;
  z-index: 0;

  .bg-shape {
    position: absolute;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.1);
    animation: float 6s ease-in-out infinite;
  }

  .bg-shape-1 {
    width: 300rpx;
    height: 300rpx;
    top: -100rpx;
    left: -50rpx;
    animation-delay: 0s;
  }

  .bg-shape-2 {
    width: 200rpx;
    height: 200rpx;
    top: 100rpx;
    right: -30rpx;
    animation-delay: 1s;
  }

  .bg-shape-3 {
    width: 150rpx;
    height: 150rpx;
    top: 250rpx;
    left: 50%;
    transform: translateX(-50%);
    animation-delay: 2s;
  }
}

@keyframes float {
  0%, 100% {
    transform: translateY(0) rotate(0deg);
  }
  50% {
    transform: translateY(-20rpx) rotate(180deg);
  }
}

/* 自定义导航栏 */
.custom-navbar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 999;
  padding-top: var(--status-bar-height, 44px);

  .navbar-content {
    height: 88rpx;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 30rpx;
  }

  .navbar-left, .navbar-right {
    width: 80rpx;
    height: 80rpx;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    transition: all 0.3s;

    &:active {
      background-color: rgba(255, 255, 255, 0.15);
      transform: scale(0.95);
    }
  }

  .navbar-title {
    flex: 1;
    text-align: center;
    color: #fff;
    font-size: 36rpx;
    font-weight: 600;
    text-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.2);
  }
}

.loading-container {
  padding: 100rpx 0;
  position: relative;
  z-index: 1;
}

.detail-content {
  padding: 20rpx;
  padding-bottom: 140rpx;
  padding-top: calc(88rpx + var(--status-bar-height, 44px) + 40rpx);
  position: relative;
  z-index: 1;
}

.demand-card, .content-card, .info-card, .stats-card {
  background: white;
  border-radius: 24rpx;
  margin-bottom: 24rpx;
  box-shadow: 0 8rpx 24rpx rgba(0, 0, 0, 0.06);
  overflow: hidden;
  transition: all 0.3s;

  &:active {
    transform: translateY(-4rpx);
    box-shadow: 0 12rpx 32rpx rgba(0, 0, 0, 0.1);
  }
}

.demand-card {
  padding: 40rpx;
  position: relative;

  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 6rpx;
    background: linear-gradient(90deg, #3cc51f 0%, #2ea516 100%);
  }
}

.demand-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 20rpx;
}

.demand-title {
  font-size: 36rpx;
  font-weight: bold;
  color: #333;
  flex: 1;
  margin-right: 20rpx;
}

.demand-status {
  padding: 8rpx 16rpx;
  border-radius: 20rpx;
  font-size: 24rpx;
  font-weight: 500;
  
  &.status-published {
    background: #f0f9ff;
    color: #3cc51f;
    border: 1rpx solid #3cc51f;
  }
  
  &.status-matched {
    background: #fff7e6;
    color: #fa8c16;
    border: 1rpx solid #ffd591;
  }
  
  &.status-closed, &.status-expired {
    background: #fff2f0;
    color: #ff4d4f;
    border: 1rpx solid #ffccc7;
  }
}

.demand-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 20rpx;
  margin-bottom: 30rpx;
}

.meta-item {
  display: flex;
  align-items: center;
  gap: 8rpx;
}

.meta-text {
  font-size: 28rpx;
  color: #666;
}

.budget-section {
  background: linear-gradient(135deg, #3cc51f 0%, #2ea516 100%);
  padding: 28rpx 32rpx;
  border-radius: 20rpx;
  color: white;
  box-shadow: 0 8rpx 20rpx rgba(60, 197, 31, 0.3);
  position: relative;
  overflow: hidden;

  &::before {
    content: '';
    position: absolute;
    top: -50%;
    right: -20%;
    width: 200rpx;
    height: 200rpx;
    background: rgba(255, 255, 255, 0.1);
    border-radius: 50%;
  }
}

.budget-title {
  font-size: 26rpx;
  opacity: 0.95;
  margin-bottom: 12rpx;
  font-weight: 500;
  position: relative;
  z-index: 1;
}

.budget-amount {
  display: flex;
  align-items: baseline;
  gap: 8rpx;
  position: relative;
  z-index: 1;
}

.currency {
  font-size: 32rpx;
  font-weight: 600;
}

.amount {
  font-size: 42rpx;
  font-weight: bold;
  text-shadow: 0 2rpx 4rpx rgba(0, 0, 0, 0.2);
}

.unit {
  font-size: 26rpx;
  opacity: 0.95;
}

.content-card, .info-card {
  padding: 30rpx;
}

.card-title {
  font-size: 32rpx;
  font-weight: bold;
  color: #333;
  margin-bottom: 20rpx;
}

.content-text {
  font-size: 30rpx;
  line-height: 1.6;
  color: #666;
}

.info-item {
  margin-bottom: 24rpx;
  
  &:last-child {
    margin-bottom: 0;
  }
}

.info-label {
  display: flex;
  align-items: center;
  gap: 8rpx;
  font-size: 28rpx;
  font-weight: 500;
  color: #333;
  margin-bottom: 8rpx;
}

.info-value {
  font-size: 28rpx;
  color: #666;
  line-height: 1.5;
}

.expire-time {
  color: #ff6b6b;
}

.time-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 12rpx;
}

.stats-card {
  display: flex;
  padding: 30rpx;
}

.stats-item {
  flex: 1;
  text-align: center;
}

.stats-number {
  font-size: 40rpx;
  font-weight: bold;
  color: #3cc51f;
  margin-bottom: 8rpx;
}

.stats-label {
  font-size: 24rpx;
  color: #999;
}

.bottom-actions {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  padding: 20rpx 24rpx;
  display: flex;
  gap: 20rpx;
  box-shadow: 0 -8rpx 24rpx rgba(0, 0, 0, 0.08);
  z-index: 1000;
}

.action-btn {
  flex: 1;
  height: 96rpx;
  border-radius: 48rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12rpx;
  font-size: 32rpx;
  font-weight: 600;
  transition: all 0.3s;
  box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.1);

  &:active {
    transform: scale(0.96);
    box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.08);
  }
}

.contact-btn {
  background: linear-gradient(135deg, #FFD700 0%, #FFA500 100%);
  color: white;
  box-shadow: 0 4rpx 12rpx rgba(255, 215, 0, 0.4);
}

.apply-btn {
  background: linear-gradient(135deg, #3cc51f 0%, #2ea516 100%);
  color: white;
  box-shadow: 0 4rpx 12rpx rgba(60, 197, 31, 0.4);
}

.safe-bottom {
  height: constant(safe-area-inset-bottom);
  height: env(safe-area-inset-bottom);
}
</style> 